﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Keyword.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections.Generic;
using IveelySE.NLP.Common.DataStructure.List;
using IveelySE.NLP.Common.DataStructure.Table;

#endregion

namespace IveelySE.NLP.Knowledge
{
    /// <summary>
    ///   知识中的关键字
    /// </summary>
    [Serializable]
    public class Keyword
    {
        #region 属性或字段

        /// <summary>
        ///   拥有该关键字的文档集合
        ///   以及关键字在其中的权值
        /// </summary>
        private readonly DoubleSortedList _list;

        /// <summary>
        ///   关键字值
        /// </summary>
        public string Value { get; set; }

        #endregion

        #region 公有方法

        public Keyword()
        {
            this._list = new DoubleSortedList();
        }

        /// <summary>
        ///   添加对应文档
        /// </summary>
        /// <param name="docId"> 文档编号 </param>
        /// <param name="rank"> 权值 </param>
        public void Add(double docId, double rank)
        {
            this._list.Add(rank, docId);
        }

        /// <summary>
        ///   获取该关键字前N个文档
        ///   时间复杂度O(n)
        /// </summary>
        /// <param name="n"> N值 </param>
        public DoubleSortedList GetTopN(int n)
        {
            var result = new DoubleSortedList();
            foreach(var docRank in _list)
            {
                if(n-- > 0)
                {
                    result.Add(docRank.Key, docRank.Value);
                }
            }
            return result;
        }

        public static IntTable<int, double> Merge(Keyword[] keywords)
        {
            var result = new IntTable<int, double>();
            foreach(Keyword keyword in keywords)
            {
                foreach(KeyValuePair<double, double> keyValuePair in keyword.GetTopN(100))
                {
                    result.Add(keyValuePair.Value, keyValuePair.Key);
                }
            }

            return result;
        }

        #endregion

        #region 私有方法

        #endregion
    }
}